<?
	session_start();
	include "funkcie/definitions.php";
	
	
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sk" lang="sk">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta http-equiv="Content-language" content="sk" />
  <link rel="stylesheet" type="text/css" href="css/styl.css" />
  <title>Zadania-BETA</title>
  <script type="text/javascript" src="funkcie/funkcie.js"></script>
 </head>
<body>
<div id="all">
<div id="content">

    <script type="text/javascript">window.location.hash="hore";</script>

<?php

    //Pripojenie
    require_once ('funkcie/functions_class.php');
    $functs = new Functions();
    $functs->connect_to_DB();

    // ODHLÁSENIE
    if ($_GET["logout"] ==1){
      session_destroy();
      session_unregister("myusername");
      session_unregister("mypassword");
      session_unregister("logged-id");
      $_SESSION["logged"]="";
      $_SESSION["logged-id"]="";
    }

    // AK BOLO NAPISANE NIECO DO MENO A HESLO
    if ($_POST['meno'] != "" && $_POST['heslo'] != "" ){

      // username and password sent from form
      $myusername=$_POST['meno'];
      $mypassword=$_POST['heslo'];
      $role=$_POST['role'];

      // To protect MySQL injection (more detail about MySQL injection)
      $myusername = stripslashes($myusername);
      $mypassword = stripslashes($mypassword);
      $myusername = mysql_real_escape_string($myusername);
      $mypassword = mysql_real_escape_string($mypassword);

      //Dotaz na DB
      $result=$functs->select_login("*","pouzivatel",$myusername,$mypassword);
      $pole=mysql_fetch_array($result);
      
      // Mysql_num_row is counting table row
      $count=mysql_num_rows($result);
      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count==1){
        // Register $myusername, $mypassword and redirect to file "login.php"
        session_register("myusername");
        session_register("mypassword");
        session_register("logged-id");
        
        
        $_SESSION["logged"]="yes";
        $_SESSION["logged-id"]=$pole['pouzivatel_id'];
        $_SESSION["logged-user"]=$myusername;
        $_SESSION["role"]=$pole['rola_id'];
        
      }else $count=2;
    }

    // TEST PREMENNEJ
    if($_SESSION["logged"]=="yes" && (session_is_registered("myusername") && session_is_registered("mypassword"))){

      if($_SESSION["role"]==1){
      //AK JE lOGNUTY UCITEL
        echo'
          
          
          <table id="menutab" cellspacing="0" cellpadding="0" border="0" align="left">
            <tr align="left">
              <td><a name="hore"></a>
                <a href="?mod=zadania"><div id="but_uc_zad"></div></a>
                <a href="?mod=list_stud"><div id="but_uc_list_stud"></div></a>
                <a href="?mod=consult_prof"><div id="but_uc_list_consult"></div></a>
                <a href="?logout=1"><div id="but_uc_logoff"></div></a>
              </td>
            </tr>
          </table>
          
          <div id="back-tile">
          <div id="cont-u"></div>
          <div id="cont-m">
        ';
        if(isset($_GET['mod'])){
          switch ($_GET['mod']){
            case "zadania" : require("zadania.php");break;
            case "edit" : require("edit.php");break;
            case "list_stud" : require("list_stud.php");break;
            case "consult_prof" : require("consult_prof.php");break;
          }
        }else{
            require("zadania.php");
        }
        echo '</div>';
        echo '<div id="cont-b"></div>';
        echo '</div>';
        
        
      }else if($_SESSION["role"]==2){
      //AK JE lOGNUTY STUDENT
          echo'
          <table id="menutab" cellspacing="0" cellpadding="0" border="0" align="left">
            <tr align="left">
              <td><a name="hore"></a>
                <a href="?mod=student"><div id="but_st_moje_zadanie"></div></a>
                <a href="?mod=prihlasenie"><div id="but_st_zadania"></div></a>
                <a href="?mod=consult_stud"><div id="but_uc_list_consult"></div></a>
                <a href="?logout=1"><div id="but_uc_logoff"></div></a>
              </td>
            </tr>
          </table>
          
          
          <div id="back-tile">
          <div id="cont-u"></div>
          <div id="cont-m">
          ';



          if(isset($_GET['mod'])){
            switch ($_GET['mod']){
              case "student" : require("student.php");break;
              case "prihlasenie" : require("prihlasenie.php");break;
              case "consult_stud" : require("consult_stud.php");break;
            }
          }else{
              require("student.php");
          }
          echo '</div>';
          echo '<div id="cont-b"></div>';
          echo '</div>';
        }
        
        echo '<div id="bottom">Všetky práva vyhradené (c) 2009 FEI - TUKE</div>';
        
      }else {
        

        echo '<form action="?" method="post">';
        echo '
        	<table id="login" border="0" cellpadding="0" cellspacing="0">
        		<tr>
        			<td valign="top"><div id="meno"></div></td><td valign="top"><input class="inph" name="meno" type="text" /></td>
        		</tr>
        		<tr>
        			<td valign="top"><div id="heslo"></div></td><td valign="top"><input class="inpd" name="heslo" type="password" /></td>
        		</tr>
        		<tr>
        			<td colspan="2" align="right"><input class="inplogin" name="submit" type="submit" value=" " /></td>
        		</tr>
        	</table>';
        echo '</form>';
        
        echo '<script type="text/javascript">document.forms[0].meno.focus();</script>';

        
        
        if($count==2)echo '<p><div class="zvyraznene">'.invalid_login_pass.'</div></p>';else
        if($_POST['meno'] == "" && $_POST['heslo'] == "" && isset($_POST['submit']) && $_POST['submit']==" ") echo '<p class="zvyraznene">'.missing_login_data.'</p>'; else
        if($_POST['meno'] == "" && $_POST['heslo'] != "" && isset($_POST['submit']) && $_POST['submit']==" ") echo '<p><div class="zvyraznene">'.missing_login.'</div></p>'; else
        if($_POST['meno'] != "" && $_POST['heslo'] == "" && isset($_POST['submit']) && $_POST['submit']==" ") echo '<p><div class="zvyraznene">'.missing_pass.'</div></p>';

        echo '<div id="login-bottom"></div>';
    }
    
  ?>
  
  <script type="text/javascript">window.location.hash="hore";</script>
  </div>
</div>
</body>
</html>